#include <iostream>
#include <cstring>
#include <map>
#include <algorithm>
using namespace std;
const int N = 2e4 + 10;
map<string, string>f;
int n, m;
string find(string s)
{
    if (s != f[s]) {
        f[s] = find(f[s]);
    }
    return f[s];
}
int main()
{
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        string a;
        cin >> a;
        f[a] = a;
    }
    while (m--) {
        int a;
        string s1, s2;
        cin >> a >> s1 >> s2;
        string s3 = find(s1);
        string s4 = find(s2);
        if (a == 1) {
            f[s3] = s4;
        }
        else {
            puts(s3 == s4 ? "1" : "0");
        }
    }
    return 0;
}